Summit.Lib.JS.load("Data.Jax");

Summit.UI.Widget.Jax_Select = function(settings)
{
	var self = this;
	
	/////////////////////////////////////////////
	/////////////////////////////// CONFIGURATION
	/////////////////////////////////////////////
	
	this.settings = {
		'feed':false,
		'action':'Search',
		'idVar':false,
		'textVar':false,
		'select':false,
		'selected':0
	}
	
	for(i in settings)
	this.settings[i] = settings[i];
	
	
	/////////////////////////////////////////////
	//////////////////////////////// JAX VARIBLES
	/////////////////////////////////////////////
	
	
	this.xvars = new Array();
	
	this.addVar = function(varName,varValue)
	{
		var newVar = {name:varName,value:varValue}
		self.xvars.push(newVar)
		
		return self;
	}
	
	/////////////////////////////////////////////
	/////////////////////////////// FUNCTIONALITY
	/////////////////////////////////////////////
	
	this.loadData = function(onload)
	{
		var Qjax = new Summit.Data.Jax();
		Qjax.feed = self.settings.feed;
		Qjax.action = self.settings.action;

		Qjax.Var('per_page',0);

		for(var i=0; i<self.xvars.length; i++)
		{
			Qjax.Var(self.xvars[i].name,self.xvars[i].value);
		}

		Qjax.handler=function(feed)
		{
			self.settings.select.empty();

			var results = feed.getElementsByTagName('results').item(0).childNodes;
			var count = 0;

			for(var i=0; i<results.length; i++)
			{
				var result = results[i];

				var newOpt = new Element('option', {
					value:result.getAttribute(self.settings.idVar)
				})
				
				newOpt.setText("#"+result.getAttribute(self.settings.idVar)+" - "+result.getAttribute(self.settings.textVar));
								
				if(parseInt(newOpt.value)==self.settings.selectedId)
				newOpt.selected=true;
				
				self.settings.select.adopt(newOpt);
			}
			
			onload.apply(self);
		}

		Qjax.Call();
	}
	
}